package com.git_taoxina.homework.test4;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Threads {
    private int tickets;
    private Lock lock = new ReentrantLock();
    public Threads() {
    }

    public Threads(int tickets) {
        this.tickets = tickets;
    }

    public void getTicket(int tickets) {
        String name = Thread.currentThread().getName();
        lock.lock();
        if (this.tickets > 0) {
            System.out.println(name + "卖了一张票");
            this.tickets--;
            System.out.println("还剩下" + this.tickets + "张票");
        } else {
            System.out.println("没票了！");
        }
        lock.unlock();
    }


    public int getTickets() {
        return tickets;
    }

    public void setTickets(int tickets) {
        this.tickets = tickets;
    }
}
